﻿using System;
using UnityEngine;
using UnityEngine.UI;

namespace Core
{
    /// <summary>
    /// 前缀 slider_XXX
    /// </summary>
    public class UISlider : UIWidget
    {
        private Slider slider;

        public event Action<float> onValueChanged;

        private float mValue = 1;
        public float Value
        {
            get => mValue;
            set
            {
                mValue = value;
                slider.value = value;
            }
        }

        public override void Awake(GameObject go)
        {
            base.Awake(go);

            slider = go.GetComponent<Slider>();
            slider.onValueChanged.AddListener(OnValueChangedHandler);
        }

        public override void Dispose()
        {
            if (IsDisposed) return;
            base.Dispose();

            slider.onValueChanged.AddListener(OnValueChangedHandler);
            slider = null;
        }

        private void OnValueChangedHandler(float arg0)
        {
            mValue = arg0;
            onValueChanged?.Invoke(arg0);
        }
    }
}
